<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class UserController extends BaseController {

    public function add(){
        $username   = I('username');
        $password   = I('password');
        $repassword = I('repassword');
        $email  = I('email');

        /* 检测密码 */
        if($password != $repassword){
            $this->outOInfo('密码和重复密码不一致!', 0);
        }

        $password   = md5(C('DATA_AUTH_KEY').$password);
        $userInfo   = D('User')->register($username, $password, $email);

        if(!is_string($userInfo))
            $this->outOInfo(array('info' => $userInfo));

        $this->outOInfo($userInfo, 0);

    }

    /**
     * 获取用户信息
     * @author  王中艺
     */
    function getInfo(){
        $userId     = $this->http['user_id'] ? $this->http['user_id'] : UID;
        $info   = D('User')->getUserInfo($userId);

        //获取版本信息
        $version =C('VERSION_NUMBER');
        $this->outOInfo(array('info' => $info,'version'=>$version));
    }

    /**
     * 会员状态修改
     * @author 朱亚杰 <zhuyajie@topthink.net>
     */
    public function changeStatus(){
        $id = array_unique((array)I('id',0));
        $method     = I('get.method');

        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->outOInfo('不允许对超级管理员执行该操作!', 0);
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->outOInfo('请选择要操作的数据!', 0);
        }

        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('User', $map );
                break;
            case 'resumeuser':
                $this->resume('User', $map );
                break;
            case 'deleteuser':
                $this->delete('User', $map );
                break;
            default:
                $this->echoAjax('参数非法!', 0);
        }
    }

    /**
     * 用户管理首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $page   = $this->http['p'] ? $this->http['p'] : 1;
        $nickname       =   I('nickname');

        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }

        $mResult    = $this->lists('User', $map,$page);
        $list   = $mResult['list'];
        foreach($list as &$user){
            $user['status_text'] = $user['status'] == 1 ? '正常' : '禁用';
            $user['last_login_ip']  = long2ip($user['last_login_ip']);
            $user['last_login_time'] = $user['last_login_time'] == 0 ? '尚未登录' : date('Y-m-d', $user['last_login_time']);
        }
        //int_to_string($list);


        $result     = array(
            'search'    => array(
                'nickname'  => $nickname,
            ),
            'users'     => $list,
            'pages'  => $mResult['pages']
        );
        $this->outOInfo($result);
    }



    /**
     * 修改昵称初始化
     * @author huajie <banhuajie@163.com>
     */
    public function updateNickname(){
        $nickname = M('User')->getFieldByUid(UID, 'nickname');
        $this->assign('nickname', $nickname);
        $this->meta_title = '修改昵称';
        $this->display('updatenickname');
    }

    /**
     * 修改昵称提交
     * @author huajie <banhuajie@163.com>
     */
    public function submitNickname(){
        //获取参数
        $nickname = I('post.nickname');
        $password = I('post.password');
        empty($nickname) && $this->error('请输入昵称');
        empty($password) && $this->error('请输入密码');

        //密码验证
        $User   =   new UserApi();
        $uid    =   $User->login(UID, $password, 4);
        ($uid == -2) && $this->error('密码不正确');

        $User =   D('User');
        $data   =   $User->create(array('nickname'=>$nickname));
        if(!$data){
            $this->error($User->getError());
        }

        $res = $User->where(array('uid'=>$uid))->save($data);

        if($res){
            $user               =   session('user_auth');
            $user['username']   =   $data['nickname'];
            session('user_auth', $user);
            session('user_auth_sign', data_auth_sign($user));
            $this->success('修改昵称成功！');
        }else{
            $this->error('修改昵称失败！');
        }
    }

    /**
     * 修改密码初始化
     * @author huajie <banhuajie@163.com>
     */
    public function updatePassword(){
        $this->meta_title = '修改密码';
        $this->display('updatepassword');
    }

    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function submitPassword(){
        $post   = file_get_contents('php://input', true);
        $post   = json_decode($post, true);
        //获取参数
        empty($post['old_pass']) && $this->outOInfo('请输入原密码', 0);
        empty($post['new_pass']) && $this->outOInfo('请输入新密码', 0);
        empty($post['re_new_pass']) && $this->outOInfo('请输入确认密码', 0);

        if($post['new_pass'] !== $post['re_new_pass']){
            $this->outOInfo('两次输入的密码不一致', 0);
        }

        $oldPass    = md5(C('DATA_AUTH_KEY').$post['old_pass']);
        $newPass    = md5(C('DATA_AUTH_KEY').$post['new_pass']);

        if(session('user.password') != $oldPass)
            $this->outOInfo('密码验证错误', 0);

        $userInfo   = session('user');
        $userInfo['password']   = $newPass;
        $ret     = D('User')->update($userInfo);
        if(!is_string($ret))
            $this->outOInfo(null);
        $this->outOInfo($ret, 0);
    }
    /**
     * 管理员修改密码
     *
     */
    public function modifyPassword(){
        //获取参数
        $password   =   I('get.password');
        $id   =   I('get.id');
        empty($password) && $this->outOInfo('请输入密码',0);
        $newPass    = md5(C('DATA_AUTH_KEY').$password);
        $status     = D('User')->modifyPwd($id, $newPass);
        if($status){
            $this->outOInfo('修改密码成功！');
        }else{
            $this->outOInfo('密码修改失败！',0);
        }
    }
    

    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }

}